/*
 *  SyncIT , easy synchronization of files between computers and devices.
 *  Copyright (C) 2011  Christian Masus <cmasus(at)gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version. If redistributing and/or modifying
 *  this file, this notice must be included in the redistributed or
 *  modified copy.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package gui.javagui.preferences;

import gui.javagui.JavaGui;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.KeyStroke;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeSelectionModel;

import config.ConfigInterface;

/**
 * Opens a JDialog where the user can change all the main settings.
 * @author Christian Masus
 *
 */
public class PreferencesDialog {

	private final String GENERAL_SETTINGS = "General";
	private final String CONNECTION_SETTINGS = "Connection";
	private final String SCHEDULER_SETTINGS = "Scheduler";

	private final JDialog dialog;
	private JTree tree;
	private JPanel mainConfigPanel;
	private JButton okButton;
	private JButton cancelButton;
	private JButton applyButton;

	/**
	 * Initiates and displays this JDialog.
	 */
	public PreferencesDialog(ConfigInterface config) {
		JFrame frame = JavaGui.getFrame();
		dialog = new JDialog(frame, "Preferences");


		JPanel navigationPanel = new JPanel();
		DefaultMutableTreeNode generalTreeNode = new DefaultMutableTreeNode(GENERAL_SETTINGS);
		DefaultMutableTreeNode connectionTreeNode = new DefaultMutableTreeNode(CONNECTION_SETTINGS);
		DefaultMutableTreeNode schedulerTreeNode = new DefaultMutableTreeNode(SCHEDULER_SETTINGS);
		Object[] treeNodes = {generalTreeNode, connectionTreeNode, schedulerTreeNode};
		tree = new JTree(treeNodes);
		tree.setPreferredSize(new Dimension(100, 300));
		tree.setBorder(BorderFactory.createLoweredBevelBorder());
		tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
		tree.putClientProperty("JTree.lineStyle", "Angled");
		DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
		Icon noIcon = null;
		renderer.setLeafIcon(noIcon);
		renderer.setClosedIcon(noIcon);
		renderer.setOpenIcon(noIcon);
		tree.setCellRenderer(renderer);
		tree.addTreeSelectionListener(treeSelectionListener);
		navigationPanel.add(tree);


		mainConfigPanel = new JPanel();
		mainConfigPanel.setLayout(new CardLayout());
		mainConfigPanel.setPreferredSize(new Dimension(500, 300));
		JPanel generalPanel = new GeneralPanel();
		JPanel connectionPanel = new ConnectionPanel();
		JPanel schedulerPanel = new SchedulerPanel();
		mainConfigPanel.add(generalPanel, GENERAL_SETTINGS);
		mainConfigPanel.add(connectionPanel, CONNECTION_SETTINGS);
		mainConfigPanel.add(schedulerPanel, SCHEDULER_SETTINGS);


		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
		buttonPanel.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.GRAY));
		buttonPanel.setPreferredSize(new Dimension(500, 35));
		okButton = new JButton("OK");
		cancelButton = new JButton(cancelAction);
		applyButton = new JButton("Apply");
		buttonPanel.add(Box.createHorizontalGlue());
		buttonPanel.add(okButton);
		buttonPanel.add(Box.createHorizontalStrut(5));
		buttonPanel.add(cancelButton);
		buttonPanel.add(Box.createHorizontalStrut(5));
		buttonPanel.add(applyButton);
		buttonPanel.add(Box.createHorizontalStrut(5));


		JPanel contentPanel = new JPanel(new BorderLayout());
		contentPanel.add(navigationPanel, BorderLayout.WEST);
		contentPanel.add(mainConfigPanel, BorderLayout.CENTER);
		contentPanel.add(buttonPanel, BorderLayout.SOUTH);
		contentPanel.setOpaque(true);
		dialog.setContentPane(contentPanel);


		dialog.pack();
		dialog.setResizable(false);
		dialog.setLocationRelativeTo(frame);

		//		dialog.getRootPane().setDefaultButton(okButton);
		dialog.getRootPane().getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "cancel");
		dialog.getRootPane().getActionMap().put("cancel", cancelAction);

		dialog.setVisible(true);

		tree.setSelectionRow(0); // TODO read this from config.
	}


	/**
	 * Action for closing this dialog without applying any changes.
	 */
	@SuppressWarnings("serial")
	Action cancelAction = new AbstractAction("Cancel") {
		public void actionPerformed(ActionEvent e) {
			dialog.setVisible(false);
			dialog.dispose();
		}
	};

	TreeSelectionListener treeSelectionListener = new TreeSelectionListener() {
		public void valueChanged(TreeSelectionEvent e) {
			DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
			if (node == null) {
				return;
			}
			Object nodeInfo = node.getUserObject();
			CardLayout cl = (CardLayout)(mainConfigPanel.getLayout());
			cl.show(mainConfigPanel, nodeInfo.toString());
		}
	};

}
